home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / ada / gwuada_9.zip / MISC.C < prev    next >
C/C++ Source or Header  |  1993-07-27  |  18KB  |  792 lines

  1. /*
  2.  * Copyright (C) 1985-1992  New York University
  3.  * 
  4.  * This file is part of the Ada/Ed-C system.  See the Ada/Ed README file for
  5.  * warranty (none) and distribution info and also the GNU General Public
  6.  * License for more details.
  7.  
  8.  */
  9. /* misc.c - miscellaneous programs */
  10.  
  11.  
  12. #include "config.h"
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "time.h"
  17. #include "ifile.h"
  18. #include "miscp.h"
  19. #ifdef BSD
  20. #include <strings.h>
  21. #endif
  22.  
  23. #ifndef LIBDIR
  24. #define LIBDIR "/usr/local/lib"
  25. #endif
  26.  
  27. #ifdef ADALIB
  28. #define EXIT_INTERNAL_ERROR
  29. #endif
  30.  
  31. #ifdef BINDER
  32. #define EXIT_INTERNAL_ERROR
  33. extern int adacomp_option;
  34. #endif
  35.  
  36. #ifdef INT
  37. #define EXIT_INTERNAL_ERROR
  38. #endif
  39.  
  40. #ifndef EXPORT
  41. #undef EXIT_INTERNAL_ERROR
  42. #endif
  43. #ifdef BSD
  44. #include <sys/file.h>
  45. #endif
  46.  
  47. char *LIBRARY_PREFIX= "";
  48.  
  49. /* PREDEFNAME gives directory path to predef files.
  50.  * libset() is used to toggle between libraries (the users and predef).
  51.  * tname = libset(lname) sets library prefix for ifopen, etc. to lname
  52.  * and returns prior setting in tname.
  53.  */
  54.  
  55. static void openerr(char *filename, char *mode);
  56.  
  57. #ifdef SMALLOC
  58. unsigned int smalloc_free = 0;
  59. char    *smalloc_ptr;
  60. #define SMALLOC_BLOCK 2000
  61. char **smalloc_table = (char **)0;
  62. unsigned smalloc_blocks = 0;
  63. #endif
  64.  
  65. char *smalloc(unsigned n)                                        /*;smalloc*/
  66. {
  67.     /* variant of malloc for use for blocks that will never be freed,
  68.      * primarily blocks used for small strings. This permits allocation
  69.      * in larger blocks avoiding the malloc overhead required for each block.
  70.      */
  71. #ifndef SMALLOC
  72.     return emalloct(n, "smalloc");
  73. #else
  74.     char *p;
  75.     if (n & 1) n+= 1;
  76. #ifdef ALIGN4
  77.     if (n & 2) n+= 2;
  78. #endif
  79.  
  80.     if (n > SMALLOC_BLOCK) { /* large block allocated separately */
  81. #ifdef DEBUG
  82.         printf("smalloc: warning block %u exceeds %d SMALLOC_BLOCK\n",
  83.           n, SMALLOC_BLOCK);
  84. #endif
  85.         p = emalloct(n, "smalloc");
  86.         return p;
  87.     }
  88.     if (n > smalloc_free) {
  89.         smalloc_ptr = emalloct(SMALLOC_BLOCK, "smalloc-block");
  90.         smalloc_free = SMALLOC_BLOCK;
  91.         smalloc_blocks++;
  92.         if (smalloc_blocks == 1) {
  93.             smalloc_table = (char **) emalloct(sizeof (char **),
  94.               "smalloc-table");
  95.         }
  96.         else { /* reallocate blocks */
  97.             smalloc_table = (char **) erealloct((char *)smalloc_table,
  98.               sizeof(char **) * (smalloc_blocks), "smalloc-table-realloc");
  99.         }
  100.         smalloc_table[smalloc_blocks-1] = smalloc_ptr;
  101.     }
  102.     p = smalloc_ptr;
  103.     smalloc_ptr += n;
  104.     smalloc_free -= n;
  105.     return p;
  106. #endif
  107. }
  108.  
  109. #ifdef DEBUG
  110. void smalloc_list()
  111. {
  112.     int i;
  113.     char **st;
  114.     st = smalloc_table;
  115.     for (i = 0; i < smalloc_blocks; i++) {
  116.         printf("%d %ld %x\n", i, *st, *st);
  117.         st++;
  118.     }
  119. }
  120. #endif
  121.  
  122. int is_smalloc_block(char *p)                            /*;is_smalloc_block*/
  123. {
  124.     /* returns TRUE is p points within block allocated by smalloc */
  125. #ifdef SMALLOC
  126.     int i;
  127.     char **st;
  128.  
  129.     st = smalloc_table;
  130.     if (smalloc_blocks == 0) chaos("is_malloc_block - no blocks");
  131.     for (i = 0; i < smalloc_blocks; i++) {
  132.         if (*st <= p && p  < (*st+(SMALLOC_BLOCK-1)))
  133.             return TRUE;
  134.         st++;
  135.     }
  136.     return FALSE;
  137. #else
  138.     return FALSE;
  139. #endif
  140. }
  141.  
  142. void capacity(char *s)                /*;capacity*/
  143. {
  144.     /* called  when compiler capacity limit exceeded.
  145.      * EXIT_INTERNAL_ERROR is defined when the module is run by itself
  146.      * (not spawned from adacomp) and DEBUG is not defined.
  147.      */
  148. #ifdef EXIT_INTERNAL_ERROR
  149.     fprintf(stderr, "capacity limit exceeded: %s\n", s);
  150.     exitp(RC_INTERNAL_ERROR);
  151. #else
  152. #ifdef DEBUG
  153.     printf("capacity limit exceeded: %s\nexecution abandoned \n", s);
  154. #endif
  155.     fprintf(stderr, "capacity limit exceeded: %s\n", s);
  156.     exitp(RC_INTERNAL_ERROR);
  157. #endif
  158. }
  159.  
  160. #ifdef CHAOS
  161. void chaos(char *s)                                                /*;chaos*/
  162. {
  163.     /* called when internal logic error detected and it is not meaningful
  164.      * to continue execution. This is never defined for the export version.
  165.      */
  166.     fprintf(stderr, "chaos: %s\nexecution abandoned \n", s);
  167.     printf("chaos: %s\nexecution abandoned \n", s);
  168.     exitp(RC_INTERNAL_ERROR);
  169. }
  170. #else
  171. void exit_internal_error()                        /*;exit_internal_error*/
  172. {
  173.     /* called when internal logic error detected and it is not meaningful
  174.      * to continue execution. This procedure is called by the export version.
  175.      * EXIT_INTERNAL_ERROR is defined when the module is run by itself
  176.      * (not spawned from adacomp) and EXPORT is defined.
  177.      * Now that adabind is a separate module which can be called by itself
  178.      * or spawned from adacomp, we must test the run time flag adacomp_option
  179.      * to determine which case it is.
  180.      */
  181. #ifdef EXIT_INTERNAL_ERROR
  182. #ifdef BINDER
  183.     if (adacomp_option)
  184. #endif
  185.         fprintf(stderr, "Adaed internal error - Please report.\n");
  186.     exit(RC_INTERNAL_ERROR);
  187. #else
  188.     exit(RC_INTERNAL_ERROR);
  189. #endif
  190. }
  191. #endif
  192.  
  193. void exitp(int n)                                                /*;exitp*/
  194. {
  195.     /* synonym for exit() used so can trap exit() calls with debugger */
  196.     exit(n);
  197. }
  198.  
  199. char *ecalloc(unsigned nelem, unsigned nsize)            /*;ecalloc */
  200. {
  201.     /* calloc with error check if no more */
  202.  
  203.     char   *p;
  204.  
  205.     if (nelem > 20000) chaos("ecalloc: ridiculous argument");
  206.  
  207.     p = calloc (nelem, nsize);
  208.     if (p == (char *) 0)
  209.         capacity("out of memory \n");
  210.     return p;
  211. }
  212.  
  213. char *emalloc(unsigned n)                                        /*;emalloc */
  214. {    /* avoid BUGS - use calloc which presets result to zero  ds 3 dec 84*/
  215.     /* malloc with error check if no more */
  216.  
  217.     char   *p;
  218.  
  219.     if (n > 50000) chaos("emalloc: ridiculous argument");
  220.     p = calloc (1, n);
  221.     if (p == (char *) 0)
  222.         capacity("out of memory \n");
  223.     return (p);
  224. }
  225.  
  226. char *erealloc(char *ptr, unsigned size)                        /*;eralloc */
  227. {
  228.     /* realloc with error check if no more */
  229.  
  230.     char   *p;
  231.  
  232.     p = realloc (ptr, size);
  233.     if (p == (char *) 0)
  234.         capacity("erealloc: out of memory \n");
  235.     return (p);
  236. }
  237.  
  238. char *strjoin(char *s1, char *s2)                                /*;strjoin */
  239. {
  240.     /* return string obtained by concatenating argument strings
  241.      * watch for either argument being (char *)0 and treat this as null string
  242.      */
  243.  
  244.     char   *s;
  245.  
  246.     if (s1 == (char *)0) s1= "";
  247.     if (s2 == (char *)0) s2 = "";
  248.     s = smalloc((unsigned) strlen(s1) + strlen(s2) + 1);
  249.     strcpy(s, s1);
  250.     strcat(s, s2);
  251.     return s;
  252. }
  253.  
  254. int streq(char *a, char *b)                                            /*;streq*/
  255. {
  256.     /* test two strings for equality, allowing for null pointers */
  257.     if (a == (char *)0 && b == (char *)0)
  258.         return TRUE;
  259.     else if (a == (char *)0 || b == (char *)0)
  260.         return FALSE;
  261.     else return (strcmp(a, b) == 0);
  262. }
  263.  
  264. char *substr(char *s, int i, int j)                                /*;substr */
  265. {
  266.     /* return substring s(i..j) if defined, else return null ptr*/
  267.  
  268.     int    n;
  269.     char    *ts, *t;
  270.  
  271.     if (s == (char *)0) return (char *) 0;
  272.     n = strlen(s);
  273.     if (!(i > 0 && j <= n && i <= j)) return (char *)0;
  274.     /* allocate result, including null byte at end */
  275.     ts = smalloc((unsigned) j - i + 2);
  276.     t = ts;
  277.     s = s + (i - 1); /* point to start of source*/
  278.     for (; i <= j; i++) *t++ = *s++; /* copy characters */
  279.     *t = '\0'; /* terminate result */
  280.     return ts;
  281. }
  282.  
  283. /* getopt(3) procedure obtained from usenet */
  284. /*
  285.  * getopt - get option letter from argv
  286.  */
  287. #ifdef IBM_PC
  288. #define nogetopt
  289. #endif
  290.  
  291. #ifdef nogetopt
  292. char   *optarg;                /* Global argument pointer. */
  293. int    optind = 0;                /* Global argv index. */
  294.  
  295. static char *scan = NULL;    /* Private scan pointer. */
  296.  
  297. int getopt(int argc, char **argv, char *optstring)                /*;getopt */
  298. {
  299.     register char   c;
  300.     register char  *place;
  301.     optarg = NULL;
  302.  
  303.     if (scan == NULL || *scan == '\0') {
  304.         if (optind == 0)
  305.             optind++;
  306.  
  307.         if (optind >= argc || argv[optind][0] != '-' || argv[optind][1] == '\0')
  308.             return (EOF);
  309.         if (strcmp (argv[optind], "--") == 0) {
  310.             optind++;
  311.             return (EOF);
  312.         }
  313.  
  314.         scan = argv[optind] + 1;
  315.         optind++;
  316.     }
  317.  
  318.     c = *scan++;
  319.     place = strchr (optstring, c);
  320.  
  321.     if (place == NULL || c == ':') {
  322.         fprintf (stderr, "%s: unknown option -%c\n", argv[0], c);
  323.         return ('?');
  324.     }
  325.  
  326.     place++;
  327.     if (*place == ':') {
  328.         if (*scan != '\0') {
  329.             optarg = scan;
  330.             scan = NULL;
  331.         }
  332.         else {
  333.             optarg = argv[optind];
  334.             optind++;
  335.         }
  336.     }
  337.     return (c);
  338. }
  339. #endif
  340.  
  341. char *greentime(int un)                                        /*;greentime*/
  342. {
  343.     /* get greenwich time in string of 23 characters.
  344.      * format of result is as follows
  345.      *    1984 10 02 16 30 36 nnn
  346.      *    123456789a123456789b123
  347.      *    year mo da hr mi se uni
  348.      *
  349.      * greenwich time is used to avoid problems with daylight savings time.
  350.      * The last three characters are the compilation unit number
  351.      * (left filled with zeros if necessary).
  352.      * NOTE: changed to use local time to give approx. same time as
  353.      * SETL versi